#!/bin/bash

# ============================================================
# OHOS 测试服务器 - Docker 容器启动脚本
# ============================================================

set -e

# 颜色定义
RESET='\033[0m'
GREEN='\033[32m'
YELLOW='\033[33m'
CYAN='\033[36m'

# 容器配置
IMAGE_NAME="ohos-test-servers:latest"
CONTAINER_NAME="ohos-servers"

# 检查是否已有同名容器在运行
if [ "$(docker ps -q -f name=${CONTAINER_NAME})" ]; then
    echo -e "${YELLOW}⚠️  容器 ${CONTAINER_NAME} 已在运行${RESET}"
    read -p "是否停止并删除现有容器? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        docker stop "${CONTAINER_NAME}"
        docker rm "${CONTAINER_NAME}"
    else
        echo -e "${YELLOW}取消启动${RESET}"
        exit 0
    fi
elif [ "$(docker ps -aq -f name=${CONTAINER_NAME})" ]; then
    echo -e "${YELLOW}⚠️  发现已停止的容器 ${CONTAINER_NAME}${RESET}"
    read -p "是否删除并重新创建? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        docker rm "${CONTAINER_NAME}"
    else
        echo -e "${CYAN}启动现有容器...${RESET}"
        docker start "${CONTAINER_NAME}"
        docker logs -f "${CONTAINER_NAME}"
        exit 0
    fi
fi

echo -e "${CYAN}========================================${RESET}"
echo -e "${CYAN}  启动 OHOS 测试服务器容器${RESET}"
echo -e "${CYAN}========================================${RESET}"
echo ""

echo -e "${CYAN}📦 镜像: ${IMAGE_NAME}${RESET}"
echo -e "${CYAN}🏷️  容器名: ${CONTAINER_NAME}${RESET}"
echo ""

# 选择运行模式
echo -e "${CYAN}请选择运行模式:${RESET}"
echo "  1) 前台模式 (交互式，Ctrl+C 停止)"
echo "  2) 后台模式 (守护进程)"
read -p "选择 (1/2) [1]: " MODE
MODE=${MODE:-1}

echo ""
echo -e "${CYAN}🚀 启动容器...${RESET}"
echo ""

# 端口映射（DNS 使用 5353 避免与系统 DNS 冲突）
PORTS="\
  -p 5353:53/udp \
  -p 1882:1882 \
  -p 1883:1883 \
  -p 3100:3100 \
  -p 3101:3101 \
  -p 4000:4000 \
  -p 4001:4001 \
  -p 4002:4002 \
  -p 4003:4003 \
  -p 4004:4004 \
  -p 4005:4005 \
  -p 4006:4006 \
  -p 8005:8005 \
  -p 8080:8080"

if [ "$MODE" = "1" ]; then
    # 前台模式
    docker run -it --rm \
        --name "${CONTAINER_NAME}" \
        ${PORTS} \
        "${IMAGE_NAME}"
else
    # 后台模式
    docker run -d \
        --name "${CONTAINER_NAME}" \
        --restart unless-stopped \
        ${PORTS} \
        "${IMAGE_NAME}"
    
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}✅ 容器已在后台启动${RESET}"
        echo ""
        echo -e "${CYAN}📋 容器信息:${RESET}"
        docker ps -f name="${CONTAINER_NAME}" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
        echo ""
        echo -e "${CYAN}🔍 查看日志:${RESET}"
        echo -e "   docker logs -f ${CONTAINER_NAME}"
        echo ""
        echo -e "${CYAN}🛑 停止容器:${RESET}"
        echo -e "   docker stop ${CONTAINER_NAME}"
        echo ""
        echo -e "${CYAN}🔄 重启容器:${RESET}"
        echo -e "   docker restart ${CONTAINER_NAME}"
        echo ""
        
        # 询问是否查看日志
        read -p "是否查看实时日志? (y/N): " -n 1 -r
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            docker logs -f "${CONTAINER_NAME}"
        fi
    else
        echo -e "${YELLOW}❌ 容器启动失败${RESET}"
        exit 1
    fi
fi